package lol.clann.minecraft.springboot.plugin.bukkit.command.resolver.factory;

import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolver;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolverFactory;
import org.bukkit.Material;
import org.bukkit.command.CommandException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

/**
 * 1.14+不让用数字id
 * @author pyz
 * @date 2019/5/6 9:29 PM
 */
@Component
@ConditionalOnExpression("${bukkit.version.number} < 11300")
public class MaterialCommandResolverFactory_v11300d implements CommandResolverFactory {
    @Override
    public boolean canResolve(Class<?> type) {
        return Material.class.isAssignableFrom(type);
    }

    @Override
    public CommandResolver getResolver(Class<?> clazz) {
        return (sender, arg, type) -> {
            Material material = null;
            try {
                int id = Integer.valueOf(arg);
                material = Material.getMaterial(id);
            } catch (NumberFormatException e) {
                material = Material.getMaterial(arg);
            }
            if (material == null) {
                throw new CommandException("该物品不存在:" + arg);
            } else {
                return material;
            }
        };
    }
}
